<template>
  <body id="paper">
    <el-form
      :model="loginForm"
      :rules="rules"
      class="login-container"
      label-position="left"
      label-width="0px"
      v-loading="loading"
    >
      <h3 class="login_title">用户注册</h3>
      <el-form-item prop="nickName">
        <el-input
          type="text"
          v-model="loginForm.nickName"
          auto-complete="off"
          placeholder="昵称"
        ></el-input>
      </el-form-item>

      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="loginForm.username"
          auto-complete="off"
          placeholder="账号"
          @change="exist"
        ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          auto-complete="off"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="repassword">
        <el-input
          type="password"
          v-model="loginForm.repassword"
          auto-complete="off"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input
          type="text"
          v-model="loginForm.email"
          auto-complete="off"
          placeholder="E-Mail"
        ></el-input>
      </el-form-item>
      <el-form-item prop="smscode" class="code">
            <el-input v-model="loginForm.smscode" placeholder="验证码" style="width:120px; float:left;"></el-input>
            <el-button type="primary" :disabled='isDisabled' @click="sendCode" style="float:right;">{{buttonText}}</el-button>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          style="width: 40%;background: #505458;border: none"
          v-on:click="register"
          >注册</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-link href="/login">已有账号？去登录</el-link>
      </el-form-item>
    </el-form>
    <div style="width:auto;margin:0 auto; padding:20px 0;">
		 		<a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51012202000940" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"> 蜀ICP备2021008536号-1<img src="../assets/img/icon/备案图标.png" style="float:left;"/><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;"> 川公网安备 51012202000940号</p></a>
		</div>
  </body>
</template>
<script>
export default {
  data() {
    let reg = /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{6,12}$/
    var newPass= (rule, value, callback) => {
    if (!reg.test(value)) {
    callback(new Error('密码应是6-12位数字、字母或字符！'))
    } else if (this.loginForm.password === value) {
    callback()
    } else if (this.loginForm.password != value) {
    callback(new Error('两次密码不一致！'))
    }
    }
    let checkEmail = (rule, value, callback) => {
      var regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
          if ( !regEmail.test(value)) {
            callback(new Error('邮箱格式不正确'))
          } else {
            callback()
          }
    }
    return {
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" },
                  { validator: newPass, trigger: 'blur' }],
        repassword:[{ required: true, message: "确认密码不能为空", trigger: "blur" },
                   { validator: newPass, trigger: 'blur' }],
        email:[{required: true, message: "邮箱不能为空", trigger: "blur" },
                  { validator: checkEmail, trigger: 'blur' }],
        smscode: [{ required:true,message:"验证码不能为空", trigger: 'blur' }],
      },
      buttonText: '发送验证码',
      isDisabled: false, // 是否禁止点击发送验证码按钮
      flag: true,
      checked: true,
      loginForm: {
        username: "",
        password: "",
        repassword: "",
        email:"",
        nickName: "",
        smscode: ""
      },
      loading: false
    };
  },
  methods: {
    sendCode () {
      var _this = this;
      let email = this.loginForm.email
      if (this.checkEmail(email)) {
        this.$axios.post('/api/api/mail', {
          mail: this.loginForm.email
        })
        .then(resp=>{
          if(resp.data.code === 200){
      let time = 60
        this.buttonText = '已发送'
        this.isDisabled = true
        if (this.flag) {
          this.flag = false;
          let timer = setInterval(() => {
            time--;
            this.buttonText = time + ' 秒'
            if (time === 0) {
              clearInterval(timer);
              this.buttonText = '重新获取'
              this.isDisabled = false
              this.flag = true;
            }
          }, 1000)
        }
          }
        })
        .catch(function (error) {
        })


      }
    },
    register() {
      var _this = this;
       const jse = new this.$jsEncrypt();
      jse.setPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArUnLWVN7YVeR7GyxZNhMnYtGa5b0TgAlffd6h7DltNDKC1t7vs9YleNF5//NZ+7ETa9TdHWHxoMf5pqhGXFZxc8aICF7WJsk1ow8tAll6CFNpagOhVEyND4NjEJdYOyPNUpvKGJlcpTJ5uoIvShXSovGClmUDOnmHN2/DbfXhtiuIHdSFfxoWPluhJtfOK8bC500qdLjUJiEWQwtPq8Ep6DECGacwAf3SzdJcDcPQ3WssI3q+jHEgzGzfAmE6jTKVkEgxCWnyAJIH7qWhoHz1feapNB6IWt7IB2ElV/jFJ4O0dZJdBCRiI9F9p6CLmObYnQPXyPZwRp13kOWOmLp9wIDAQAB")
      this.$axios
        .post("/register", {
          username: this.loginForm.username,
          password: jse.encrypt(this.loginForm.password),
          nickName: this.loginForm.nickName,
          email: this.loginForm.email,
          code:this.loginForm.smscode
        })
        .then(resp => {
          if (resp.data.code === 200) {
            this.$alert("注册成功", "提示", {
              confirmButtonText: "确定"
            });
            _this.$router.replace("/login");
          } else {
            this.$alert(resp.data.message, "提示", {
              confirmButtonText: "确定"
            });
          }
        })
        .catch(failResponse => {});
    },
    exist() {
      var _this = this;
      this.$axios
        .post("/exist", {
          username: this.loginForm.username
        })
        .then(resp => {
          if (resp.data.code === 200) {
            // alert(resp.data.message);
          }else {
            this.$message({
                  message: '用户名已被注册',
                  type: 'error'
              })
                this.loginForm.username = ''
          }
        })
        .catch(function(error) {

        });
    },
    checkEmail(str) {
      let re = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
      if (re.test(str)) {
        return true;
      } else {
        return false;
      }
    }
   }
};
</script>
<style>
#paper {
  background: url("../assets/img/bg/a1.jpg") no-repeat;
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
}
body {
  margin: 0px 0px;
}
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.login_remember {
  margin: 0px 0px 35px 0px;
  text-align: left;
}
.code >>> .el-form-item__content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.code button {
  margin-left: 10px;
  width: 140px;
  text-align: center;
}
.el-button--primary:focus {
  background: #409EFF;
  border-color: #409EFF;
  color: #fff;
}
</style>
